//
//  HotViewController.m
//  Delicious
//
//  Created by gy on 16/5/10.
//  Copyright © 2016年 gy. All rights reserved.
//

#import "HotViewController.h"
#import "WaterFlowLayout.h"
#import "CollectionViewCell.h"
#import "Collection.h"
#import "HotDetailViewController.h"
#import "AppDelegate.h"

@interface HotViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, WaterFlowLayoutDelegate>
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) NSMutableArray *hotArr; // 来存放hot的model
@property (nonatomic, assign) double height;

@end

@implementation HotViewController

- (NSMutableArray *)hotArr{
    if (!_hotArr) {
        _hotArr = [NSMutableArray array];
    }
    return _hotArr;
}
- (void)viewWillAppear:(BOOL)animated{
//    self.tabBarController.tabBar.hidden = YES;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"本周最热";
    // Do any additional setup after loading the view.
    WaterFlowLayout *waterLayout = [[WaterFlowLayout alloc]init];
    // 设置间隔
    waterLayout.itemSpacing = 10;
    // 设置分区偏移
    waterLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // 设置列数
    waterLayout.numberOfColumns = 2;
    // 设定itemSize
    CGFloat width =  ([UIScreen mainScreen].bounds.size.width - waterLayout.sectionInset.left - waterLayout.sectionInset.right - (waterLayout.numberOfColumns - 1) * waterLayout.itemSpacing) / waterLayout.numberOfColumns;
    waterLayout.itemSize = CGSizeMake(width, 0);
    // 设置代理
    waterLayout.delegate = self;
    
    _collection = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:waterLayout];
    _collection.delegate = self;
    _collection.dataSource = self;
    [_collection registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    [self getData];

}

- (void)getData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:kMainHot parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"];
        for (NSDictionary *d in arr) {
            Collection *model = [[Collection alloc]init];
            [model setValuesForKeysWithDictionary:d];
            [self.hotArr addObject:model];
        }
        [self.collection reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
// WaterFlowLayout 代理
- (CGFloat)waterFlowLayout:(WaterFlowLayout *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath{
//    CGFloat height = 0;
//    height = arc4random() % 270 + 172;
    return 200;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.hotArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
       cell.model = self.hotArr[indexPath.row];
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    Collection *model = self.hotArr[indexPath.row];
    HotDetailViewController *vc = [[HotDetailViewController alloc]init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
    

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
